package com.gjy.map;

import java.util.LinkedList;

/**
 * @author 宫静雨
 * @version 1.0
 * @since 2023-04-17 16:47:15
 */
public class MapTest1 {

    /**
     * 位置实体类，根据自己的来即可
     */
    private static class Position {

        /**
         * 纬度
         */
        private Double latitude;

        /**
         * 经度
         */
        private Double longitude;

        public Double getLatitude() {
            return latitude;
        }

        public void setLatitude(Double latitude) {
            this.latitude = latitude;
        }

        public Double getLongitude() {
            return longitude;
        }

        public void setLongitude(Double longitude) {
            this.longitude = longitude;
        }

        public Position(Double latitude, Double longitude) {
            this.latitude = latitude;
            this.longitude = longitude;
        }
    }

    /**
     * 取几个经纬度的中心点
     *
     * @param postionList 经纬度的集合
     */
    public static void getCenterPoint(LinkedList<Position> postionList) {
        int total = postionList.size();
        double X = 0, Y = 0, Z = 0;

        while (!postionList.isEmpty()) {
            Position g = postionList.pollFirst();
            if (g != null) {
                double lat, lon, x, y, z;
                lat = g.getLatitude() * Math.PI / 180;
                lon = g.getLongitude() * Math.PI / 180;
                x = Math.cos(lat) * Math.cos(lon);
                y = Math.cos(lat) * Math.sin(lon);
                z = Math.sin(lat);
                X += x;
                Y += y;
                Z += z;
            }
        }

        X = X / total;
        Y = Y / total;
        Z = Z / total;
        double Lon = Math.atan2(Y, X);
        double Hyp = Math.sqrt(X * X + Y * Y);
        double Lat = Math.atan2(Z, Hyp);
        double longitude = Lon * 180 / Math.PI;
        double latitude = Lat * 180 / Math.PI;
        System.out.println("中心点经度：" + longitude);
        System.out.println("中心点纬度：" + latitude);

    }


    public static void main(String[] args) {
        LinkedList<Position> list = new LinkedList<>();
        list.add(new Position(35.1719916700000040, 119.2328202000000200));
        list.add(new Position(35.1319216700000040, 119.2925302000000200));
        list.add(new Position(35.1919216700000040, 119.2625302000000200));

        getCenterPoint(list);
    }
}
